package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;//在新增、删除、修改菜品的时候都要清理redis中的数据(分类)

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<Object> add(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.addWithFlavor(dishDTO);

        //清理缓冲数据
        String key="dish_"+dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * dish pageQuery
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * get by id
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * alter dish
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result<Object> update(@RequestBody DishDTO dishDTO) {
        dishService.updateWithFlavor(dishDTO);

        //清理缓冲
        //将所有的菜品缓冲数据清理 dish_
        cleanCache("dish_*");
        //修改菜品时候如果修改的是分类，会影响到两个缓存数据，所以需要全部删除
        return Result.success();
    }

    /**
     * select by categoryId
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> getByType(Long categoryId) {
        List<Dish> dishList = dishService.getByType(categoryId);
        return Result.success(dishList);
    }

    /**
     * 菜品启售、停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品启售、停售")
    public Result<Object> updateStatus(@PathVariable Integer status, Long id) {
        dishService.updateStatus(status, id);

        //清理缓冲
        //将所有的菜品缓冲数据清理 dish_
        cleanCache("dish_*");
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result<Object> delete(Long[] ids) {
        log.info("菜品批量删除：{}",ids);
        dishService.delete(ids);
        //将所有的菜品缓冲数据清理 dish_
        cleanCache("dish_*");
        //为什么清理所有：因为不知道当前id的dish属于哪一个分类下，所以可能会删除不同分类下的菜品，这样就可能导致多个分类
        // 出现异常，然且只能在查询过后才知道分类id，这样还要查询数据库就得不偿失
        return Result.success();
    }

    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
